package com.jinlejia.erp.ui.new_house.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.utils.SharePTool;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 选择楼盘Adapter
 */
public class SelecHousesAdapter extends YfListAdapter<Map<String, String>> {
    private Context context;

    public SelecHousesAdapter(ArrayList<Map<String, String>> data) {
        super(data);
    }

    @Override
    public void setData(ArrayList<Map<String, String>> data) {
        if (selectHistory!=null && selectHistory.size()>0){
            for (Map<String, String> map:data) { //如果要填充的数据，有用户所选中的数据，则进行展示
                Map<String, String> selectedMap = selectHistory.get(map.get("code"));
                if (selectedMap!=null){
                    if ("true".equalsIgnoreCase(selectedMap.get("selected"))){
                        map.put("selected",String.valueOf(true));
                    }
                }
            }
        }

        super.setData(data);
    }

    @Override
    public void addData(ArrayList<Map<String, String>> data) {
        if (selectHistory!=null && selectHistory.size()>0){
            for (Map<String, String> map:data) { //如果要填充的数据，有用户所选中的数据，则进行展示
                Map<String, String> selectedMap = selectHistory.get(map.get("code"));
                if (selectedMap!=null){
                    if ("true".equalsIgnoreCase(selectedMap.get("selected"))){
                        map.put("selected",String.valueOf(true));
                    }
                }
            }
        }
        super.addData(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selec_houses_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(final RecyclerView.ViewHolder viewHolder, final int i) {
        ((ViewHolder) viewHolder).name.setText(mData.get(i).get("name"));
        if (mData.get(i).containsKey("commissionType") && !TextUtils.isEmpty( mData.get(i).get("commissionType") ) && !"null".equals(mData.get(i).get("commissionType"))) {
            if ( (int) Double.parseDouble(mData.get(i).get("commissionType")) == 0) {
                ((ViewHolder) viewHolder).commission.setText(mData.get(i).get("commissionStr") );
            } else if ( (int) Double.parseDouble(mData.get(i).get("commissionType")) == 1) {
                ((ViewHolder) viewHolder).commission.setText(mData.get(i).get("commissionStr") + "%");
            } else if ( (int) Double.parseDouble(mData.get(i).get("commissionType")) == 2) {
                ((ViewHolder) viewHolder).commission.setText(mData.get(i).get("commissionStr") + "元/套");
            }else if ((int) Double.parseDouble(mData.get(i).get("commissionType")) == 3){
                ((ViewHolder) viewHolder).commission.setText(mData.get(i).get("commissionStr") + "元/㎡");
            }
        }else{
            ((ViewHolder) viewHolder).commission.setText(mData.get(i).get("commissionStr"));
        }
        //计算距离
        LatLng p1LL = new LatLng(Double.parseDouble(mData.get(i).get("lat")), Double.parseDouble(mData.get(i).get("lng")));
        LatLng p2LL = new LatLng(SharePTool.getLatitude(context), SharePTool.getLongitude(context));
        DecimalFormat df = new DecimalFormat("######0.00");
        ((ViewHolder) viewHolder).distance.setText(df.format(DistanceUtil.getDistance(p1LL, p2LL) / 1000) + "km");
        if( "true".equals(mData.get(i).get("selected"))){
            ((ViewHolder) viewHolder).checkBox.setChecked(true);
        }else{
            ((ViewHolder) viewHolder).checkBox.setChecked(false);
        }
        viewHolder.itemView.setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new YfSimpleViewHolder(view);
    }

    private Map<String,Map<String,String>> selectHistory;
    public void addChecked(Map<String,String> map){
        if (selectHistory==null){
            selectHistory = new HashMap<>();
        }
        boolean selected = Boolean.parseBoolean(map.get("selected"));
        map.put("selected",String.valueOf(!selected));
        if (!selected){
            selectHistory.put(map.get("code"),map);
        }else {
            selectHistory.remove(map.get("code"));
        }
        //仅对更改的数据进行UI更新
        for (int i = 0; i < mData.size(); i++) {
            Map<String, String> cuMap = mData.get(i);
            if (TextUtils.equals(cuMap.get("code"),map.get("code"))){
                cuMap.put("selected",map.get("selected"));
                notifyItemChanged(i);
            }
        }
    }


    public ArrayList<Map<String,String>> getSelectMap() {
//        return selectMap; //将所有选择的数据进行返回，包括可见的数据
        ArrayList<Map<String,String>> maps = new ArrayList<>();
        for (Map<String, String> map:mData) {

            if ("true".equalsIgnoreCase(map.get("selected"))){
                Map<String,String> newMap = new HashMap<>();
                newMap.put("code",map.get("code"));
                newMap.put("name",map.get("name"));

                if ( map.containsKey("commissionType")){
                    if (map.get("commissionType").equals("1")){
                        newMap.put("commission", map.get("commissionStr") + "%");
                    }else if (map.get("commissionType").equals("2")){
                        newMap.put("commission", map.get("commissionStr") + "元/套");
                    }else if (map.get("commissionType").equals("3")){
                        newMap.put("commission", map.get("commissionStr") + "元/㎡");
                    }
                }
                //计算距离
                LatLng p1LL = new LatLng(Double.parseDouble(map.get("lat")), Double.parseDouble(map.get("lng")));
                LatLng p2LL = new LatLng(SharePTool.getLatitude(context), SharePTool.getLongitude(context));
                DecimalFormat df = new DecimalFormat("######0.00");
                newMap.put("distance", df.format(DistanceUtil.getDistance(p1LL, p2LL) / 1000) + "km");
                maps.add(newMap);
            }
        }
        return maps;
    }

    private static final class ViewHolder extends RecyclerView.ViewHolder {

        CheckBox checkBox;
        TextView name;
        TextView distance;
        TextView commission;

        public ViewHolder(View itemView) {
            super(itemView);
            checkBox = (CheckBox) itemView.findViewById(R.id.id_check_box);
            name = (TextView) itemView.findViewById(R.id.id_name);
            distance = (TextView) itemView.findViewById(R.id.id_distance);
            commission = (TextView) itemView.findViewById(R.id.id_commission);
        }
    }


}
